<template>
  <div class="transactions-container">
    <!-- 交易记录表格 -->
    <div class="table-container">
      <el-table :data="transactionsList" style="width: 100%" v-loading="loading" :empty-text="'データがありません'" class="transactions-table">
        <el-table-column prop="order_number" label="取引番号" width="200" header-align="left" align="left" style="font-size: 12px;" show-overflow-tooltip />
        <el-table-column prop="createtime" label="時間" width="180" header-align="left" align="left">
          <template #default="scope">
            {{ formatDate(scope.row.createtime) }}
          </template>
        </el-table-column>
        <el-table-column prop="money" label="金額" width="150" header-align="left" align="left">
          <template #default="scope">
            <span >
              {{ formatMoney(scope.row.money) }}円
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="after" label="残高" width="150" header-align="left" align="left">
          <template #default="scope">
            {{ formatMoney(scope.row.after) }}円
          </template>
        </el-table-column>
        <el-table-column prop="memo" label="詳細" min-width="200" header-align="left" align="left" show-overflow-tooltip>
          <template #default="scope">
            <span :title="scope.row.memo">
              {{ scope.row.memo && scope.row.memo.length > 11 ? scope.row.memo.substring(0, 11) + '...' : scope.row.memo }}
            </span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 移动端卡片列表 -->
      <div class="mobile-card-list" v-loading="loading">
        <div v-for="transaction in transactionsList" :key="transaction.id" class="mobile-card">
          <div class="card-header">
            <div class="card-number">{{ transaction.order_number }}</div>
          </div>
          <div class="card-content">
              <div class="card-row">
              <span class="card-label">取引番号：</span>
              <span class="card-value">{{ transaction.order_number }}</span>
            </div>
            <div class="card-row">
              <span class="card-label">取引日時：</span>
              <span class="card-value">{{ formatDate(transaction.createtime) }}</span>
            </div>
            <div class="card-row">
              <span class="card-label">金額：</span>
              <span class="card-value" >
                {{ formatMoney(transaction.money) }}円
              </span>
            </div>
            <div class="card-row">
              <span class="card-label">ポスト残高：</span>
              <span class="card-value">{{ formatMoney(transaction.after) }}円</span>
            </div>
            <div class="card-row memo-row">
              <span class="card-label">詳細：</span>
              <span class="card-value memo-content">{{ transaction.memo }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-container">
        <span class="pagination-info">共 {{ totalCount }} 条</span>
        <el-pagination 
          v-model:current-page="currentPage" 
          v-model:page-size="pageSize" 
          :page-sizes="[20]" 
          layout="prev, pager, next" 
          :total="totalCount" 
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" 
          class="custom-pagination" />
        <span class="pagination-info">前页 {{ currentPage }} 页</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { userApi } from '../../../api/index'

// 数据列表
const transactionsList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const totalCount = ref(0)

// 获取资金明细列表
const getTransactionsList = async () => {
  try {
    loading.value = true
    const params = {
      page: currentPage.value,
      limit: pageSize.value
    }

    const res = await userApi.getMoneyDetails(params)

    if (res.code === 1 && res.data) {
      transactionsList.value = res.data.list || []
      totalCount.value = parseInt(res.data.totalCount) || 0
    } else {
      ElMessage.error(res?.msg || 'データの取得に失敗しました')
    }
  } catch (error) {
    console.error('获取资金明细失败:', error)
    ElMessage.error('データの取得に失敗しました')
  } finally {
    loading.value = false
  }
}

// 格式化金额
const formatMoney = (amount) => {
  if (!amount) return '0'
  return amount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// 格式化日期
const formatDate = (timestamp) => {
  if (!timestamp) return ''

  const date = new Date(timestamp * 1000)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')

  return `${year}-${month}-${day} ${hours}:${minutes}`
}

// 处理每页显示数量变化
const handleSizeChange = (val) => {
  pageSize.value = val
  getTransactionsList()
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTransactionsList()
}

// 页面加载时获取数据
onMounted(() => {
  getTransactionsList()
})
</script>

<style scoped>
.transactions-container {
  padding: 20px;
  background-color: #ffffff;
  min-height: 100vh;
  max-width: 1020px;
  margin: 0 auto;
}

.table-container {
  background-color: white;
  /* border-radius: 8px; */
  overflow: hidden;
  /* box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); */
}

.transactions-table {
  border: none;
}

.transactions-table :deep(.el-table__header) {
  background-color: #4a90e2;
}

.transactions-table :deep(.el-table__header th) {
  background-color: #4a90e2 !important;
  color: white !important;
  font-weight: bold;
  border: none;
  padding: 15px 0;
}

.transactions-table :deep(.el-table__body tr) {
  border-bottom: 1px solid #e8e8e8;
}

.transactions-table :deep(.el-table__body tr:hover) {
  background-color: #f8f9fa;
}

.transactions-table :deep(.el-table__body td) {
  border: none;
  padding: 12px 0;
  color: #333;
}

.transactions-table :deep(.el-table__empty-text) {
  color: #999;
}

.pagination-container {
  margin-top: 0;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: white;
  border-top: 1px solid #e8e8e8;
}

.pagination-info {
  color: #666;
  font-size: 14px;
}

.custom-pagination {
  margin: 0;
}

.custom-pagination :deep(.el-pagination__total),
.custom-pagination :deep(.el-pagination__sizes) {
  display: none;
}

.custom-pagination :deep(.btn-prev),
.custom-pagination :deep(.btn-next) {
  background-color: #4a90e2;
  color: white;
  border: 1px solid #4a90e2;
}

.custom-pagination :deep(.btn-prev:hover),
.custom-pagination :deep(.btn-next:hover) {
  background-color: #357abd;
  border-color: #357abd;
}

.custom-pagination :deep(.el-pager li) {
  background-color: white;
  color: #4a90e2;
  border: 1px solid #ddd;
  margin: 0 2px;
}

.custom-pagination :deep(.el-pager li:hover) {
  background-color: #4a90e2;
  color: white;
}

.custom-pagination :deep(.el-pager li.active) {
  background-color: #4a90e2;
  color: white;
  border-color: #4a90e2;
}

.positive {
  color: #67c23a;
  font-weight: bold;
}

.negative {
  color: #f56c6c;
  font-weight: bold;
}

/* 设置取引番号列的字体大小 */
.transactions-table :deep(.el-table__body td:first-child),
.transactions-table :deep(.el-table__header th:first-child) {
  font-size: 12px !important;
}

/* PC端隐藏移动端卡片列表 */
.mobile-card-list {
  display: none;
}

/* 移动端响应式样式 */
@media (max-width: 768px) {
  .transactions-container {
    padding: 10px;
  }

  /* 隐藏PC端表格 */
  .transactions-table {
    display: none;
  }

  /* 显示移动端卡片列表 */
  .mobile-card-list {
    display: block;
  }

  .mobile-card {
    background: white;
    border-radius: 8px;
    margin-bottom: 12px;
    padding: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 1px solid #e8e8e8;
  }

  .card-header {
    /* background-color: #f5f5f5; */
    padding: 8px 12px;
    margin: -16px -16px 12px -16px;
    border-radius: 8px 8px 0 0;
    border-bottom: 1px solid #e8e8e8;
  }

  .card-number {
    font-size: 14px;
    font-weight: bold;
    color: #333;
    font-weight: 400;
  }

  .card-content {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .card-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 4px 0;
  }

  .card-label {
    font-size: 14px;
    color: #666;
    font-weight: 500;
    min-width: 80px;
    flex-shrink: 0;
  }

  .card-value {
    font-size: 14px;
    color: #333;
    text-align: right;
    flex: 1;
  }

  .memo-row {
    align-items: flex-start;
  }

  .memo-content {
    text-align: left;
    word-break: break-all;
    line-height: 1.4;
  }

  .positive {
    color: #67c23a;
    font-weight: bold;
  }

  .negative {
    color: #f56c6c;
    font-weight: bold;
  }

  /* 移动端分页样式调整 */
  .pagination-container {
    padding: 10px;
    flex-direction: column;
    gap: 10px;
  }

  .pagination-info {
    font-size: 12px;
  }

  .custom-pagination :deep(.el-pagination) {
    justify-content: center;
  }

  .custom-pagination :deep(.btn-prev),
  .custom-pagination :deep(.btn-next),
  .custom-pagination :deep(.el-pager li) {
    min-width: 32px;
    height: 32px;
    line-height: 32px;
    font-size: 12px;
  }
}
</style>